import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router"

export const Layout = () => import("@/layout/index.vue")

export const menuRoutes: RouteRecordRaw[] = [
  {
    path: "/dashboard",
    component: () => import("@/views/dashboard/index.vue"),
    name: "Dashboard", // 用于 keep-alive, 必须与SFC自动推导或者显示声明的组件name一致
    meta: {
      title: "看板",
      icon: "homepage",
      affix: true,
      keepAlive: true,
    },
  },
  {
    path: "/table",
    name: "Table1",
    component: () => import("@/views/demo/table.vue"),
    meta: {
      title: "列表",
      icon: "",
      keepAlive: true,
    },
  },
  {
    path: "/table2",
    name: "Table2",
    component: () => import("@/views/demo/table.vue"),
    meta: {
      title: "列表2",
      icon: "",
      keepAlive: true,
    },
  },
]

// 静态路由
export const baseRoutes: RouteRecordRaw[] = [
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },
  {
    path: "/register",
    component: () => import("@/views/login/register.vue"),
    meta: { hidden: true },
  },
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      ...menuRoutes,
      {
        path: "401",
        component: () => import("@/views/error-page/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error-page/404.vue"),
        meta: { hidden: true },
      },
    ],
  },
]

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory(),
  routes: baseRoutes as RouteRecordRaw[],
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" })
}

export default router
